package com.example.luke.cashorderingsystem.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.luke.cashorderingsystem.Model.Good;
import com.example.luke.cashorderingsystem.Model.GoodType;
import com.example.luke.cashorderingsystem.R;
import com.example.luke.cashorderingsystem.Util.GlobalFunction;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by luke on 16/11/2016.
 */

public class GoodAdapter extends BaseAdapter {
   private LayoutInflater inflater;
   private ArrayList<Good>goodArrayList;

    public GoodAdapter(Context context){
       inflater=LayoutInflater.from(context);
        goodArrayList=new ArrayList<>();

    }
    @Override
    public long getItemId(int position) {
        return 0;
    }

    public void setGoodArrayList(ArrayList<Good> goodArrayList) {
        this.goodArrayList = goodArrayList;
        notifyDataSetChanged();

    }

    public ArrayList<Good> getGoodArrayList() {
        return goodArrayList;
    }
    public void refresh(ArrayList<Good>goodArrayList){
        this.goodArrayList.clear();
        this.goodArrayList=goodArrayList;
        notifyDataSetChanged();

    }

    public void addJsonObject(JSONObject object){
        try {
            goodArrayList.add(new Good(object.getString("id"),object.getString("name"),object.getString("pinYinID")));
            notifyDataSetChanged();

        }catch (Exception e){

            GlobalFunction.printTextByTag(e.getMessage());
        }

    }
    @Override
    public int getCount() {
        return goodArrayList.size();

    }

    @Override
    public Good getItem(int position) {
        return goodArrayList.get(position);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView==null){
            convertView=inflater.inflate(R.layout.good_row,null);

        }

        Good good=getItem(position);

        TextView goodName=(TextView)convertView.findViewById(R.id.good_name);
        goodName.setText(good.getName());

        TextView goodID=(TextView)convertView.findViewById(R.id.goodID);
        goodID.setText(good.getId());

        TextView priceIn=(TextView)convertView.findViewById(R.id.priceIn);
        priceIn.setText(good.getPriceOut()+"元");

        TextView storedNumber=(TextView)convertView.findViewById(R.id.storedNumber);
        storedNumber.setText("x"+good.getStoredNumber());

        return convertView;

    }
}
